package controllers

import (
	"books_manage_sys/models"
	"encoding/json"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
)

type BookController struct {
	beego.Controller
}

func (c *BookController) Create() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	var book models.Book
	book.Title = c.GetString("title")
	book.Data = c.GetString("data")
	book.Author = c.GetString("author")
	book.Type = "待发布"
	book.Class = c.GetString("class")
	var AirTips string
	if book.Title == "" {
		AirTips += "标题不得为空;"
	}
	if book.Data == "" {
		AirTips += "内容不得为空;"
	}
	if book.Author == "" {
		AirTips += "作者不得为空;"
	}
	if book.Class == "" {
		AirTips += "板块不得为空;"
	}
	if AirTips != "" {
		c.Data["json"] = map[string]any{
			"Code": 500,
			"Msg":  AirTips,
		}
		c.ServeJSON()
		return
	}
	if book.GetBookByTitle() != nil {
		c.Data["json"] = map[string]any{
			"Code": 500,
			"Msg":  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	if book.ID != 0 {
		c.Data["json"] = map[string]any{
			"Code": 500,
			"Msg":  "文章已存在",
		}
		c.ServeJSON()
		return
	}
	if book.CreateBook() != nil {
		c.Data["json"] = map[string]any{
			"Code": 500,
			"Msg":  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	key := fmt.Sprintf("books_id:%d", book.ID)
	jsondata, _ := json.Marshal(&book)
	if models.Rdb.Set(models.Ctx, key, jsondata, -1).Err() != nil {
		c.Data["json"] = map[string]any{
			"Code": 500,
			"Msg":  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = map[string]any{
		"Code": 200,
		"Msg":  "文章上传成功",
	}
	c.ServeJSON()
}
func (c *BookController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	var FromFindBookList models.Book
	var BookList []models.Book
	var err error
	title := c.GetString("title")
	Type := c.GetString("type")
	class := c.GetString("class")
	page, _ := c.GetInt("page", 1)
	key := fmt.Sprintf("book_list:title=%s,type=%s,class=%s,page=%d", title, Type, class, page)
	jsondata, _ := models.Rdb.Get(models.Ctx, key).Result()
	if jsondata != "" {
		err = json.Unmarshal([]byte(jsondata), &BookList)
		if err != nil {
			c.Data["json"] = map[string]any{
				"Code": 500,
				"Msg":  "服务器繁忙",
			}
			c.ServeJSON()
			return
		}
	} else {
		BookList, err = FromFindBookList.GetBookList(title, class, Type, page)
		if err != nil {
			c.Data["json"] = map[string]any{
				"Code": 500,
				"Msg":  "服务器繁忙",
			}
			c.ServeJSON()
			return
		}
		setdata, _ := json.Marshal(&BookList)
		models.Rdb.Set(models.Ctx, key, setdata, -1)
	}
	c.Data["json"] = map[string]any{
		"Code": 200,
		"Msg":  "获取成功",
		"Data": BookList,
	}
	c.ServeJSON()
	return
}
func (c *BookController) Update() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	var book models.Book
	book.Title = c.GetString("title")
	book.Data = c.GetString("data")
	book.Author = c.GetString("author")
	book.Type = c.GetString("type")
	book.Class = c.GetString("class")
	id, _ := c.GetInt("id")
	book.ID = uint(id)
	if book.UpdatesBook() != nil {
		c.Data["json"] = map[string]any{
			"Code": 500,
			"Msg":  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	if book.GetBookByTitle() != nil {
		c.Data["json"] = map[string]any{
			"Code": 500,
			"Msg":  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	jsonData, _ := json.Marshal(&book)
	key := fmt.Sprintf("books_id:%d", book.ID)
	if models.Rdb.Set(models.Ctx, key, jsonData, -1).Err() != nil {
		c.Data["json"] = map[string]any{
			"Code": 500,
			"Msg":  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = map[string]any{
		"Code": 200,
		"Msg":  "修改成功",
	}
	c.ServeJSON()
}
func (c *BookController) Del() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	var book models.Book
	id, _ := c.GetInt("id")
	book.ID = uint(id)
	if book.DeleteBookByID() != nil {
		c.Data["json"] = map[string]any{
			"Code": 500,
			"Msg":  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	key := fmt.Sprintf("books_id:%d", book.ID)
	if models.Rdb.Del(models.Ctx, key).Err() != nil {
		c.Data["json"] = map[string]any{
			"Code": 500,
			"Msg":  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = map[string]any{
		"Code": 200,
		"Msg":  "删除成功",
	}
	c.ServeJSON()
}
